import { Injectable } from '@angular/core';

import { Context } from './model/context.contract';
import { StorageService } from '../storage/storage.service';

Injectable()
export class ContextService {
    public static CurrentContext: Context;
    storageService: StorageService;
    static ins: any = (<any>window);
    constructor() {
    }

    public static initialContext(token: string, user: any, pilot: any): void {
        ContextService.CurrentContext = new Context();
        StorageService.setSesstionStorage({ key: "token", value: token });
        StorageService.setSesstionStorage({ key: "user", value: user });
        //StorageService.setSesstionStorage({ key: "pilot", value: pilot });
        this.ins.pilot = pilot;
        ContextService.CurrentContext.token = StorageService.getSessionStorageByKey("token");
        ContextService.CurrentContext.user = StorageService.getSessionStorageByKey("user");
        ContextService.CurrentContext.pilot = this.ins.pilot;
    }

    public static updateContextItem(...pairs) {
        for (let pair of pairs) {
            StorageService.setSesstionStorage(pair);
            ContextService.CurrentContext[pair.key] = pair.value;
            if (pair.key === 'pilot') {
                this.ins.pilot = pair.value;
            }
        }
    }
}